using Engine;
using Engine.Media;
using Game;

namespace RuthlessConquest {
    public class BevelledButtonWidget : ButtonWidget {
        public BevelledRectangleWidget RectangleWidget { get; }

        public RectangleWidget ImageWidget { get; }

        public LabelWidget LabelWidget { get; }

        public ClickableWidget ClickableWidget { get; }

        public override bool IsClicked => ClickableWidget.IsClicked;

        public override bool IsChecked {
            get => ClickableWidget.IsChecked;
            set => ClickableWidget.IsChecked = value;
        }

        public override bool IsAutoCheckingEnabled {
            get => ClickableWidget.IsAutoCheckingEnabled;
            set => ClickableWidget.IsAutoCheckingEnabled = value;
        }

        /*public override bool IsOkButton
        {
            get
            {
                return ClickableWidget.IsOkButton;
            }
            set
            {
                ClickableWidget.IsOkButton = value;
            }
        }

        public override bool IsCancelButton
        {
            get
            {
                return ClickableWidget.IsCancelButton;
            }
            set
            {
                ClickableWidget.IsCancelButton = value;
            }
        }*/

        public override string Text {
            get => LabelWidget.Text;
            set => LabelWidget.Text = value;
        }

        public override BitmapFont Font {
            get => LabelWidget.Font;
            set => LabelWidget.Font = value;
        }

        public Subtexture Subtexture {
            get => ImageWidget.Subtexture;
            set {
                ImageWidget.Subtexture = value;
                ImageWidget.IsVisible = value != null;
            }
        }

        public override Color Color { get; set; }

        public Color DisabledColor { get; set; }

        public Color BevelColor {
            get => RectangleWidget.BevelColor;
            set => RectangleWidget.BevelColor = value;
        }

        public Color CenterColor {
            get => RectangleWidget.CenterColor;
            set => RectangleWidget.CenterColor = value;
        }

        public float AmbientLight {
            get => RectangleWidget.AmbientLight;
            set => RectangleWidget.AmbientLight = value;
        }

        public float DirectionalLight {
            get => RectangleWidget.DirectionalLight;
            set => RectangleWidget.DirectionalLight = value;
        }

        public float BevelSize { get; set; }

        public string Sound {
            get => ClickableWidget.SoundName;
            set => ClickableWidget.SoundName = value;
        }

        public BevelledButtonWidget() {
            CanvasWidget root = new() { Margin = new Vector2(6f, 6f) };
            RectangleWidget = new BevelledRectangleWidget { Texture = Textures.Gui.Panel };
            root.Children.Add(RectangleWidget);
            StackPanelWidget stackPanelWidget = new() { HorizontalAlignment = WidgetAlignment.Center, VerticalAlignment = WidgetAlignment.Center };
            ImageWidget = new RectangleWidget { IsVisible = false };
            stackPanelWidget.Children.Add(ImageWidget);
            stackPanelWidget.Children.Add(LabelWidget = new LabelWidget { FontScale = 1.25f });
            root.Children.Add(stackPanelWidget);
            ClickableWidget = new ClickableWidget { SoundName = "RCSounds/Click" };
            root.Children.Add(ClickableWidget);
            Children.Add(root);
            Color = Colors.Fore;
            DisabledColor = Colors.ForeDisabled;
            BevelSize = 2f;
        }

        public override void MeasureOverride(Vector2 parentAvailableSize) {
            LabelWidget.Color = IsEnabledGlobal ? Color : DisabledColor;
            ImageWidget.FillColor = IsEnabledGlobal ? Color : DisabledColor;
            if (ClickableWidget.IsPressed || IsChecked) {
                RectangleWidget.BevelSize = -0.5f * BevelSize;
            }
            else {
                RectangleWidget.BevelSize = BevelSize;
            }
            base.MeasureOverride(parentAvailableSize);
        }
    }
}